package cn.hxzy.servlet.admin;


import cn.hxzy.entity.User;
import cn.hxzy.entity.vo.PageObj;
import cn.hxzy.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/showUser")
public class ShowUserServlet extends HttpServlet {
    UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            PageObj<User> pageObj = new PageObj<>();
            String page = req.getParameter("page");
            String size = req.getParameter("size");
            if (page != null && page != "") {
                pageObj.setPage(Integer.parseInt(page));
            }
            if (size != null && size != "") {
                pageObj.setSize(Integer.parseInt(size));
            }
            PageObj<User> all = userService.findAll(pageObj);
            System.out.println("**************输出的信息\n"+all+"\n****************");
            req.setAttribute("showUser",all);
            req.getRequestDispatcher("/WEB-INF/jsp/admin/showUser.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String id = req.getParameter("userId");
            String name = req.getParameter("userName");
            PageObj<User> pageObj = new PageObj<>();

            if (id != null && id != "") {//id查询
                PageObj<User> user = userService.findByIdWithUser(pageObj,Integer.parseInt(id));
                System.out.println(user);
                req.setAttribute("showUser", user);
                req.getRequestDispatcher("/WEB-INF/jsp/admin/showUser.jsp").forward(req, resp);
            }
            else if (name != null && name != "") {//名字查询
                PageObj<User> user = userService.findByNameWithUser(pageObj,name);
                System.out.println(user);
                req.setAttribute("showUser", user);
                req.getRequestDispatcher("/WEB-INF/jsp/admin/showUser.jsp").forward(req, resp);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
